﻿using System;
using System.Data;
using System.Text;
using System.Xml;
using System.Web;
using QzEbook.AccessObject;
using QzEbook.Common;
using System.Collections.Generic;

public partial class admin_Feed : System.Web.UI.Page
{
    private const string BOOKS = "books";
    private const string PAGES = "pages";
    private const string CHARGE = "charge";
    private const string HOTS = "hots";
    private const string NEW = "new";

    private XmlTextWriter writer;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(Request["Uid"]))
        {
            int uid = int.Parse(Request["Uid"]);
            getRss(BOOKS, uid);
            return;
        }
        if (!String.IsNullOrEmpty(Request["Bid"]))
        {
            int bid = int.Parse(Request["Bid"]);

            getRss(PAGES, bid);
            return;

        }
        if (!String.IsNullOrEmpty(Request["Type"]))
        {
            getRss(Request["Type"], 0);
        }

    }

    private void getRss(string type, int id)
    {
        Response.ContentType = "text/xml";
        Response.Clear();
        writer = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);

        switch (type)
        {
            case BOOKS:
                getUserBooksRSS(id);
                break;
            case PAGES:
                getSingleBookRss(id);
                break;
            case HOTS :
                getBookRss("最热门杂志","BComments DESC,BHit DESC");
                break;
            case NEW:
                getBookRss("最新出版","BID DESC");
                break;
            case CHARGE :
                //getBookRss("最新书店", "BComments DESC,BHit DESC");
                break;
            default:
                break;
        }

        writer.Flush();
        Response.End();
    }

    /// <summary>
    /// 最新电子书RSS;
    /// </summary>
    private void getBookRss(String title,String orderBy)
    {
        Book book = new Book();
        string defaultFilterString = "BIsVisible<>0 and BAuditing=3";
        List<Book> booklist=book.selectTopOrderBy(defaultFilterString,orderBy);

        RssHelper.WriteRSSPrologue(writer,title, "default.xhtml","由用户最新发布通过验证的杂志集");
        RendBookData(booklist);
        RssHelper.WriteRSSClosing(writer);
 
    }

    /// <summary>
    /// 取得一本书所有的页的RSS操作;
    /// </summary>
    /// <param name="bid"></param>
    private void getSingleBookRss(int bid)
    {
        BookPage pages = new BookPage();
        pages.BookId = bid;
        List<BookPage> pagelist = pages.selectList();

        Book book = new Book();
        book.BookId = bid;
        book=book.select();

        string detailPath=string.Format("detail.xhtml?Bid={0}" , book.BookId);
        RssHelper.WriteRSSPrologue(writer, book.Title, detailPath, book.BookId.ToString());
        if (book.Price > 0)
        {
            RssHelper.AddRSSItem(writer, "此书为收费电子书", detailPath, "pages", "<h3>可通过网银支付</h3>登陆后.通过网银支付.<br />你的账号,将被加入到些书的订阅列表中.<br />将有一个时间段你可进行无限制阅读", DateTime.Now);
        }
        else
        {
            foreach (BookPage pageItem in pagelist)
            {
                string title = pageItem.Title;
                string url = string.Format("PageView.xhtml?Pid={0}", pageItem.PageId);
                string thumbPath = String.Format("{0}{1}", Config.UserBookSavePath, pageItem.Path);

                string discription = string.Format("<div><img src=\"{0}\" /></div>", thumbPath);
                DateTime date = pageItem.PostDate;

                RssHelper.AddRSSItem(writer, title, url, "pages", discription, date);
            }
        }
        RssHelper.WriteRSSClosing(writer);
    }

    /// <summary>
    /// 取得一个用户所有的书的Rss操作;
    /// </summary>
    /// <param name="p"></param>
    private void getUserBooksRSS(int p)
    {
        Book books = new Book();
        
        SiteUser userinfo = new SiteUser();
        userinfo.Uid = p;
        userinfo=userinfo.select();

        List<Book> bookList= books.bindTable(books.selectUser(p));
        RssHelper.WriteRSSPrologue(writer, userinfo.Name, "user.xhtml?Uid=" + userinfo.Uid, userinfo.LevelName);
        this.RendBookData(bookList);
        RssHelper.WriteRSSClosing(writer);
    }

    private void RendBookData(List<Book> bookList)
    {
        foreach (Book book in bookList)
        {
            string title = book.Title.ToString();
            string url = string.Format("detail.xhtml?Bid={0}", book.BookId);
            string thumbPath = String.Format("{0}{1}", Config.UserBookSavePath, book.Thumb);
            string category=Category.getName(book.CategoryId);

            StringBuilder discriptionSB = new StringBuilder();
            discriptionSB.AppendLine("<div>");
            discriptionSB.AppendFormat("<a href=\"{1}\"><img src=\"{0}\" /></a>", thumbPath, url);
            discriptionSB.AppendLine("<dl>");
            discriptionSB.AppendFormat("<dt><strong>相关信息</strong></dt>");
           
            discriptionSB.AppendFormat("<dd><strong>类型:</strong>{0}</dd>", Category.getLinkHtml(book.CategoryId));
            discriptionSB.AppendFormat("<dd><strong>关键字:</strong>{0}</dd>", book.getTagListHTML());
            discriptionSB.AppendFormat("<dd><strong>点击:</strong>{0} ,<strong>留言:</strong>{1} </dd>", book.Hit, book.Comments);
            discriptionSB.AppendLine("</dl>");
            discriptionSB.AppendLine("</div>");
            DateTime date = (DateTime)book.PostDate;

            RssHelper.AddRSSItem(writer, title, url, category, discriptionSB.ToString(), date);
        }
    }



}
